package com.gz.common;



import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.List;



import com.gz.common.model.Applicationrecord;
import com.gz.common.model.User;

/**
 * 分销service
 * @author 尹公子
 *
 */

import com.jfinal.plugin.activerecord.Page;

public class ApplicationrecordService {

	private static ApplicationrecordService service;

    private ApplicationrecordService() {
    }

    public static ApplicationrecordService getService() { 
        if (service == null) {
            service = new ApplicationrecordService();
        }
        return service;
    }

	/**
	 * @author 尹公子
	 * 添加申请分销记录
	 */
	public boolean addingApplicationRecords(Applicationrecord applicationrecord) {
		try {
			applicationrecord.save();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 根据用户id分页查询审核记录
	 * @param userId
	 * @param page
	 * @param num
	 * @param agree，1代表未审核，2代表同意和不同意，0代表全部
	 * @return Page<Applicationrecord>
	 */
	public Page<Applicationrecord> getApplicationrecordList(String userId,Integer agree,Integer pageNumber,Integer pageSize){
		StringBuffer sb=new StringBuffer();
		//分页查询参数select
		String select="select app.*,rec.username as 'recorduserName',rec.mobile as 'recorduserMobile',acc.username as 'acceptorName' ";
		//分页查询参数sqlExceptSelect
		String sql = "from tb_user as rec \r\n" + 
				"INNER JOIN applicationrecord as app \r\n" + 
				"ON(rec.id=app.recorduser)\r\n" + 
				"INNER JOIN tb_user as acc \r\n" + 
				"ON(app.acceptor=acc.id)\r\n" + 
				"where app.acceptor=?";
		sb.append(sql);
		if(agree!=0) {
			//查询未审核的
			if(agree==1) {
				sb.append("and app.agree=0");
			}
			//查询同意和不同意的
			if(agree==2) {
				sb.append("and app.agree in (1,2)");
			}
		}
		sb.append(" order by desc");
		System.out.println(sb.toString());
		return Applicationrecord.dao.paginate(pageNumber, pageSize,select,sb.toString(),userId);
	}

	/**
	 * @author ZhangZhangyi
	 * Description:传入userid查询需要当前用户审核记录
	 * @param acceptor 需要当前用户审核id
	 * @return List<Applicationrecord>
	 */
    public List<Applicationrecord> getApplicationrecordsByUserId(int acceptor) {
		return Applicationrecord.dao.find("select * from applicationrecord where acceptor=? and agree=0",acceptor);
	}
	
	/**
	 * 查询上级是否存在
	 */
	public User findSuperiorId(String mobile) {
		return User.dao.findFirst("select * from tb_user where mobile=? and usergrade !=0",mobile);
	}
	/**
	 * 	 通过id查询用户等级
	 */
	public User findDJ(Integer id) {
		return User.dao.findFirst("select * from tb_user where id=?",id);
	}
	
	/**
	 * 查询上级用户
	 */
	public User findSJ(Integer recorduser,Integer recordrank) {
		return User.dao.findFirst("select * from tb_user where id = ( SELECT acceptor FROM applicationrecord WHERE recorduser=? AND agree = 1 AND recordrank = ?)",recorduser,recordrank);
	}

    /**
     * @author ZhangZhangyi
     * Description:传入applicationrecord对象进行修改审核状态
     * @param applicationrecord 审核记录
     * @return 是否修改成功
     */
    public boolean Mvapplicationrecord(Applicationrecord applicationrecord) {
    	return applicationrecord.update();
    }
    
    /**
     * @author ZhangZhanyi
     * Description:传入审核编号查询审核记录
     * @param recordid 审核编号
     * @return 审核记录
     */
    public Applicationrecord findApplicationrecord(int recordid) {
		return Applicationrecord.dao.findFirst("select * from applicationrecord where recordid=?",recordid);
	}
    
    /**
     * 判断用户是否是分销商以及是否正在审核中
     * @param acceptor
     * @return Applicationrecord
     */
    public Applicationrecord findIsAgree(Integer acceptor) {
    	List<Applicationrecord> applicationrecord = Applicationrecord.dao.find("select * from applicationrecord where recorduser=? ORDER BY startingtime desc",acceptor);
    	//如果返回结果长度为0说明该用户可以申请成为分销商
    	return applicationrecord.size()>0?applicationrecord.get(0):null;
    }
    
    /**
     * 修改用户等级加上级
     */
    public boolean xiugaiuser(User user) {
    	return user.update();
    }
    
    /**
     * 通过申请编号拆查询user信息
     */
    public User findUserRecordid(Integer recordid) {
    	return User.dao.findFirst("select * from tb_user where id = ( select recorduser from applicationrecord where recordid = ?)",recordid);
    }
    
    /**
     * 通过申请编号查询申请表信息
     */
    public Applicationrecord findAppli(Integer recordid) {
    	return Applicationrecord.dao.findFirst("SELECT * FROM applicationrecord WHERE recordid=?",recordid);
    }

}